package com.group.eams.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.AssetDetailQueryDto;
import com.group.eams.pojo.dto.RepairRecordQueryDto;
import com.group.eams.pojo.entity.*;
import com.group.eams.pojo.entity.RepairRecord;
import com.group.eams.pojo.entity.RepairRecord;
import com.group.eams.pojo.entity.RepairRecord;
import com.group.eams.pojo.vo.FixedAssetListVo;
import com.group.eams.pojo.vo.RepairRecordListVo;
import com.group.eams.service.RepairRecordService;
import com.group.eams.service.RepairRecordService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 维修记录(RepairRecord)表控制层
 *
 * @author lqk
 * @since 2024-10-30 12:58:29
 */
@RestController
@RequestMapping("/repairRecord")
public class RepairRecordController {
    /**
     * 服务对象
     */
    @Autowired
    private RepairRecordService repairRecordService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "资产维修列表")
    public Result list(RepairRecordQueryDto repairRecordQueryDto) {

        //1.准备分页条件
        Page<RepairRecord> page = new Page<>(repairRecordQueryDto.getPage(), repairRecordQueryDto.getLimit());

        //2.设置查询条件
        Page<RepairRecordListVo> repairRecordByPage = repairRecordService.findRepairRecordByPage(page, repairRecordQueryDto);

        return Result.buildSuccess(repairRecordByPage.getTotal(), repairRecordByPage.getRecords());
    }

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找资产维修")
    public Result find(@RequestParam Long id) {
        RepairRecord repairRecord = repairRecordService.getById(id);
        return Result.buildSuccess(repairRecord);
    }

    @RequestMapping(value = "/findByRepairRecordNum", method = RequestMethod.GET)
    @ApiOperation(value = "根据单号查找资产记录")
    public Result findByRepairRecordNum(@RequestParam String repairRecordNum) {
        List<RepairRecord> repairRecord = repairRecordService.findByRepairRecordNum(repairRecordNum);
        return Result.buildSuccess(repairRecord);
    }

    @RequestMapping(value = "/findDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查找资产报修详细")
    public Result findDetail(AssetDetailQueryDto assetDetailQueryDto) {
        //设置查询单号
        assetDetailQueryDto.setAssetReportNum(assetDetailQueryDto.getAssetReportNum());
        Page<RepairRecord> page = new Page<>(assetDetailQueryDto.getPage(), assetDetailQueryDto.getLimit());
        Page<FixedAssetListVo> repairRecordDetailByPage =
                repairRecordService.findDetailByPage(page, assetDetailQueryDto);
        return Result.buildSuccess(repairRecordDetailByPage.getTotal(), repairRecordDetailByPage.getRecords());
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加资产维修")
    public Result add(@RequestBody RepairRecord repairRecord) {
        boolean result = repairRecordService.save(repairRecord);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改资产维修")
    public Result update(@RequestBody RepairRecord repairRecord) {
        boolean result = repairRecordService.updateById(repairRecord);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ApiOperation("删除资产维修")
    public Result delete(@RequestParam Long id) {
        boolean result = repairRecordService.removeById(id);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchRemove", method = RequestMethod.POST)
    @ApiOperation("批量删除资产维修")
    public Result batchDelete(@RequestBody BatchDeleteDto batchDeleteDto) {
        boolean result = repairRecordService.removeByIds(batchDeleteDto.getIds());
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ApiOperation("查询所有资产维修")
    public Result findAll() {
        List<RepairRecord> repairRecordList = repairRecordService.list();
        return Result.buildSuccess(repairRecordList);
    }

    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
    @ApiOperation("批量添加保修记录")
    public Result batchAdd(@RequestBody List<RepairRecord> repairRecords) {
        boolean result = repairRecordService.saveBatch(repairRecords);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchUpdate", method = RequestMethod.POST)
    @ApiOperation("批量修改保修记录")
    public Result batchUpdate(@RequestBody List<RepairRecord> repairRecords) {
        boolean result = repairRecordService.updateBatchById(repairRecords);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }
}


